package com.bilibili.juc.cf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @auther zzyy
 * @create 2022-01-17 18:44
 */
public class CompletableFutureOfThenCombineDemo {
    public static void main(String[] args) {
        CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t -----A come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(Thread.currentThread().getName()+"\t -----A over   " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return 10;
        });
        CompletableFuture<Integer> futureB = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t -----B come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(Thread.currentThread().getName()+"\t -----B over    " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return 20;
        });
        System.out.println(futureA.thenCombine(futureB, (a, b) -> {
            System.out.println(Thread.currentThread().getName()+"\t -----结果开始合并 " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return a + b;
        }).join());//30

    }
}
